//
//  JSTTableScreenView.m
//  KYKJPackDemo
//
//  Created by 01 on 16/11/6.
//  Copyright © 2016年 蔡正军. All rights reserved.
//

#import "JSTTableScreenView.h"
typedef void(^removeBlock)();
@interface JSTTableScreenView ()

@property (nonatomic, strong) removeBlock block;

@property (nonatomic, strong) JSTHomePageModel *homeModel;

@end
@implementation JSTTableScreenView
- (void)dealloc {
    _block = nil;
}

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void) createContent:(JSTHomePageModel *)model removeView:(void (^)())remove {
    _homeModel = model;
    NSString *imgUrl = [NSString string];
    NSArray *data = [JSTFMDBManager querySqlforTab:[JSTTableScreenData modelWith:nil getImgUrl:nil]];
    if (data.count != 0) {
        JSTTableScreenData *model = data[0];
        if (IsStrEmpty(model.imgUrl)) {
            remove();
            [self removeFromSuperview];
            return;
        }
        imgUrl = model.imgUrl;
    }else{
        remove();
        [self removeFromSuperview];
        return;
    }
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.frame = self.bounds;
    [imgView.layer setCornerRadius:5];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    imgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeNewPage)];
    [imgView addGestureRecognizer:tap];
    [imgView.layer setCornerRadius:5];
    [self addSubview:imgView];
    self.block = ^(){
        remove();
    };
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * currentDic = [userDefaults valueForKey:@"crossScreenData"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"event"] = @"view";
    [JSTServer requestPostStatisticsData:dict postInterface:currentDic[@"url"] dataDic:^(NSDictionary *dataDic) {
    } errorStr:nil];
}

- (void) changeNewPage
{
    if (_block) {
        _block();
    }
    [self removeFromSuperview];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary * currentDic = [userDefaults valueForKey:@"crossScreenData"];
    [_homeModel createOtherAdvertOclick:currentDic];
}

- (void) removeCurrentView {
    if (_block) {
        _block();
    }
    [self removeFromSuperview];
}
@end
